import javax.swing.border.*;
import javax.swing.JPanel;

public class ImageChoiceGamePanel extends JPanel{

	private static final int PANEL_WIDTH = 800 , PANEL_HEIGHT = 700;
	
	private Question question; //holds the current question
	private JPanel gameMainLeftPanel =  new JPanel(); //Container that holds all the containers at left hand side
    private JPanel gameMainRightPanel =  new JPanel(); //Container that holds all the containers at right hand side
    private DragPanel dragPanel;
	
    	
    //inner class for the ImageChoiceGamePanel
	public class DragPanel extends JPanel{
		
		private javax.swing.ImageIcon answerBoxImage = new javax.swing.ImageIcon(getClass().getResource("image/answerBox.png")); ////holds the image of the answer box
		
		private int dragX; //holds the X coordinate that being dragged
		private int dragY; //holds the Y coordinate that being dragged
		private int draggedX; //holds the X coordinate after dragging
		private int draggedY; //holds the Y coordinate after dragging
		
		private int ansBoxX = PANEL_WIDTH/2 + 100; //holds the X coordinate of the answer box
		private int ansBoxY = 50; //holds the Y coordinate of the answer box

		public DragPanel(){
			setBorder(javax.swing.BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			setBackground(new java.awt.Color(200,200,250));
			setPreferredSize(new java.awt.Dimension(PANEL_WIDTH,PANEL_HEIGHT/2-50)); //sets the panel's size to size specified
			
			addMouseListener(new java.awt.event.MouseAdapter(){
				public void mousePressed(java.awt.event.MouseEvent e){
					dragX = e.getX();
					dragY = e.getY();
					for(int i=0; i<question.getChoices().length; i++){
						updateSelecting(question.getChoices()[i]);
					}
				}
				public void mouseReleased(java.awt.event.MouseEvent e){
					
					for(int i=0; i<question.getChoices().length; i++){
						if(question.getChoices()[i].isSelected() && isDraggedToAnswerBox())
							javax.swing.JOptionPane.showMessageDialog(null, i==question.getCorrectChoiceIndex()?"Correct!":"Incorrect!");
					}
					clear();
				}
			});
			
			addMouseMotionListener(new java.awt.event.MouseMotionAdapter(){
				public void mouseDragged(java.awt.event.MouseEvent e){
					draggedX = e.getX();
					draggedY = e.getY();
					
					for(int i=0; i<question.getChoices().length; i++){
						if(question.getChoices()[i].isSelected() && isChoiceWithinTheBounds((ImageChoice)(question.getChoices()[i]))){
								updateChoicePosition((ImageChoice)(question.getChoices()[i]));
							dragX = draggedX;
							dragY = draggedY;
						}
					}
					repaint();	
				}
			});
			
		}
		
	
		public void paintComponent(java.awt.Graphics g){
			super.paintComponent(g);
			g.clearRect(0,0,WIDTH,HEIGHT);

			g.setColor(java.awt.Color.BLUE);
			java.awt.Font font = new java.awt.Font("Courier", java.awt.Font.BOLD, 20);
			g.setFont(font);
			
			g.drawString("Answer :" ,PANEL_WIDTH/2 + 100, 50);
			g.drawImage(answerBoxImage.getImage(), ansBoxX, ansBoxY, answerBoxImage.getIconWidth(), answerBoxImage.getIconHeight(),this);
			
			for(int i=0; i<question.getChoices().length; i++)
				drawImageChoice(g,(ImageChoice)(question.getChoices()[i]));
			
		}
	
		
		/*
	     * Function takes no argument.
	     * Function sets all the states "selected" of the choices to be false.
	     * Function returns nothing.
	     */
	     
		public void clear(){
			for(int i=0; i<question.getChoices().length; i++)
				question.getChoices()[i].setSelected(false);
		}
		
		/*
	     * Function takes a single argument: 
	     * ImageChoice - contains its x and y coordinates.
	     * Function returns whether the imageChoice is dragged by the user.
	     */
		public boolean isDragging(ImageChoice imageChoice){
			return dragX >= imageChoice.getX() && dragX <= imageChoice.getX() + imageChoice.getImageContent().getIconWidth() && dragY >= imageChoice.getY() && dragY <= imageChoice.getY() + imageChoice.getImageContent().getIconHeight();
		}
		
		
		/*
	     * Function takes a single argument: 
	     * Choice - contains an attribute "selected" needed to be updated in this method.
	     * Function sets the attribute "selected" to be true if the choice is dragged from the user; otherwise, the result is false.
	     * Function returns nothing.
	     */
		public void updateSelecting(Choice choice){
			if(isDragging((ImageChoice)choice))
				choice.setSelected(true);
		}
		
		/*
	     * Function takes no argument: 
	     * Function returns whether the drag montion is finish within the answer box.
	     */
		public boolean isDraggedToAnswerBox(){
			return draggedX >= ansBoxX && draggedX <= ansBoxX + answerBoxImage.getIconWidth() && draggedY >= ansBoxY && draggedY <= ansBoxY + answerBoxImage.getIconWidth();
		}
		
		/*
	     * Function takes a single argument: 
	     * ImageChoice - contains x and y coordinates needed to be updated  in this method.
	     * Function returns nothing.
	     */
		public void updateChoicePosition(ImageChoice imageChoice){
			imageChoice.setX( imageChoice.getX() + draggedX - dragX);
			imageChoice.setY( imageChoice.getY() + draggedY - dragY);
		}
		
		/*
	     * Function takes a single argument: 
	     * ImageChoice - contains x and y coordinates needed to be referred in this method.
	     * Function returns whether this choice is within the bounds of the panel.
	     */
		public boolean isChoiceWithinTheBounds(ImageChoice imageChoice){
			return imageChoice.getX()+ draggedX - dragX >=0 && imageChoice.getX()+imageChoice.getImageContent().getIconWidth() + draggedX - dragX <= dragPanel.getWidth() && imageChoice.getY()+ draggedY - dragY >=0 && imageChoice.getY() + imageChoice.getImageContent().getIconHeight() + draggedY - dragY <= dragPanel.getHeight();
		}
		
		public void drawImageChoice(java.awt.Graphics g,ImageChoice imageChoice){
			if(imageChoice.isVisible())
				g.drawImage(imageChoice.getImageContent().getImage(), imageChoice.getX(), imageChoice.getY(), imageChoice.getImageContent().getIconWidth(),imageChoice.getImageContent().getIconHeight(),this);
		}
	}
	
	public ImageChoiceGamePanel(){
		
		setPreferredSize(new java.awt.Dimension(PANEL_WIDTH,PANEL_HEIGHT)); //sets the panel's size to size specified
		
		this.question = Main.getCurrentQuestion();
		setChoicePosition();
		
		setLayout(new java.awt.BorderLayout());
		
		gameMainRightPanel.add(new HintButtonPanel(),java.awt.BorderLayout.NORTH);
		gameMainLeftPanel.setLayout(new java.awt.BorderLayout());
		gameMainLeftPanel.add(new javax.swing.JLabel(new javax.swing.ImageIcon("image/mc.jpg")),java.awt.BorderLayout.NORTH);
		gameMainLeftPanel.add(new javax.swing.JButton(question.getQuestionContent()),java.awt.BorderLayout.CENTER);
		
		add(gameMainLeftPanel,java.awt.BorderLayout.CENTER);
		add(gameMainRightPanel,java.awt.BorderLayout.EAST);
		
		dragPanel = new DragPanel();
		add(dragPanel,java.awt.BorderLayout.SOUTH);
	}
	
	public void setChoicePosition(){
		for(int i=0; i<question.getChoices().length; i++){
			((ImageChoice)(question.getChoices()[i])).setX(50+(i%2)*200);
			((ImageChoice)(question.getChoices()[i])).setY(20+(i>1?1:0)*150);
		}
	}
	
}

